package org.crawutils;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.httpclient.Cookie;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.Element;

/**
 * 请求具体网址 每个请求模块对应一个Request（注意，这里不是指的每次请求）
 * 
 * @author duyf
 * 
 */
public class Request {

	
	private HttpClient httpClient;

	private Map<String,IBeforOrAfterHandler> handlerMap=new HashMap<String, IBeforOrAfterHandler>();
	
	public Request() {

	}

	public Request(HttpClient httpClient) {
		this.httpClient = httpClient;
	}

	/**
	 * 执行请求
	 * 
	 * @param target
	 *            配置文件的前缀名
	 * @param paramMap
	 *            用户自定义的启动参数
	 */
	public Map<String, Object> execute(String target, Map<String, Object> paramMap) {
		if(httpClient==null){
			httpClient=new HttpClient();
		}
		return processStep(target,paramMap);
	}

	/**
	 * 处理每一个配置文件中所定义的所有的步骤
	 * 
	 * @param target
	 *            此参数是只 目标配置文件,通常是配置文件的名称
	 * @return
	 */
	private Map<String, Object> processStep(String target, Map paramMap) {
		// 返回值
		Map<String, Object> returnMap = new HashMap<String, Object>();
		
		// 每一步的描述
		String describe = "";
		try {
			String targetxml = target + ".xml";
			Long lastModifyDate = RequestConfig.getInstance()
					.getLastModifyDate(targetxml);
			if (lastModifyDate == null) {
				// 引发一个新的运行时异常,这样的话就会跳转到相应的500 提示界面
				// 引发一个新的运行时异常,这样的话就会跳转到相应的500 提示界面
				throw new IllegalArgumentException("请确认是否有 " + targetxml
						+ " 此配置文件,如果是新增的配置文件请重启应用");
			}

			String confPath_target = RequestConfig.getInstance()
					.getConfigPath()
					+ File.separator + targetxml;
			File file = new File(confPath_target);
			if (lastModifyDate != null && file != null) {
				if (file.lastModified() != lastModifyDate) {
					// 如果文件有改变就从新读取
					RequestConfig.getInstance().loadAgentConfFile();
				}
			}

			// 判断请求的目标是有对应的配置文件
			Document document = RequestConfig.getInstance().getDocument(targetxml);
			if (document != null) {
				Element element = document.getRootElement();
				if (null != element) {
					// 获取host
					String host = element.attribute("host") != null ? element
							.attribute("host").getValue() : null;

					// 获取端口
					String port = element.attribute("port") != null ? element
							.attribute("port").getValue()
							: null;

					//获取协议		
					String protocol=element.attribute("protocol") != null ? element
							.attribute("protocol").getValue()
							: null;		
					if(host!=null && port!=null && protocol!=null){
						httpClient.getHostConfiguration().setHost( host , Integer.parseInt(port), protocol );	
					}		
							
					// 定义httpclient 对象
					httpClient.getParams().setCookiePolicy(
							CookiePolicy.BROWSER_COMPATIBILITY);

					List<Element> els = element.elements();
					for (Element element2 : els) {

						// 处理表达式,判断是否需要停止当前程序直接返回
						if (element2.element("isStopStep") != null) {
							String expression = element2.element("isStopStep")
									.attribute("expression") != null ? element2
									.element("isStopStep").attribute(
											"expression").getValue() : null;
							if (StringUtils.isBlank(expression)) {
								throw new java.lang.IllegalArgumentException(
										"isStopStep tag expression value is null");
							} else {
								boolean flag = false;
								flag = processLogicalJudgement(expression,
										paramMap, flag);
								// 判断是否需要停止执行所有步骤
								if (flag) {
									break;
								}
							}

						}

						// 获取stepID
						String stepId = element2.attribute("id") != null ? element2
								.attribute("id").getValue()
								: null;
						// 可以在调用时指定执行的步骤，比如出现当出现意外情况，前面已经执行，重新发起执行时不需要执行前面的步骤
						String stepIdByReq = (String) paramMap
								.get(RequestConfig.STEPID);
						String stepFlag = paramMap.get(RequestConfig.STEPFLAG) == null ? ""
								: (String) paramMap.get(RequestConfig.STEPFLAG);

						// 判断是否指定了开始 step
						if (StringUtils.isNotBlank(stepIdByReq)
								&& !stepFlag.equals("1")) {
							// 如果当前步骤不是指定的步骤那么久跳出本次请求
							if (!(StringUtils.isNotBlank(stepId) && stepId
									.equals(stepIdByReq))) {
								continue;
							}

							returnMap.put(RequestConfig.STEPFLAG, "1");
						}

						// 处理当前标签是否 include 标签！如果是将递归调用 processStep
						// 进行包含标签中的指定的配置文件进行处理
						if (element2.getName().equals("include")) {
							String includeFileName = element2
									.attribute("value") != null ? element2
									.attribute("value").getValue() : null;
							if (StringUtils.isBlank(includeFileName)) {
								throw new java.lang.IllegalArgumentException(
										"include tag value property is null");
							} else {
								processStep(includeFileName, paramMap);
								// 处理完后跳出本次 循环接着走include后面的步骤
								System.out.println("不符合指定的 step 当前step将不执行");
								continue;
							}

						}
						// 获取当前步骤调整的 url
						String url = element2.attribute("url") != null ? element2
								.attribute("url").getValue()
								: null;

						// 获取当前步骤 完成的功能描述
						describe = element2.attribute("describe") != null ? element2
								.attribute("describe").getValue()
								: null;
						// 获取当前步骤 请求的方式 两种 post get
						String method = element2.attribute("method") != null ? element2
								.attribute("method").getValue()
								: null;
						// 获取当前步骤 执行后需要执行的 目标类
						String afterHandler = element2
								.attribute("AfterHandler") != null ? element2
								.attribute("AfterHandler").getValue() : null;

						// 判断URL是否来至于上一次请求之后的 alterReturn 的返回值
						if (StringUtils.isNotBlank(url)
								&& url.indexOf("#map") != -1) {
							url = url.substring(5, url.length());
							Object mapurls=paramMap.get(url);
							if(mapurls instanceof String){
								String urls=(String)mapurls;
								String urlStrs[] = urls.split(",");
								if (urlStrs != null && urlStrs.length > 0) {
									for (String string : urlStrs) {
										// 执行请求
										exeStep(paramMap,method, httpClient,
												element2, string, afterHandler);
									
									}
								}
							}else if(mapurls instanceof List){
								List<String> urllist=(List<String>)mapurls;
								for (String string : urllist) {
									// 执行请求
									exeStep(paramMap,method, httpClient,
											element2, string, afterHandler);
								
								}
							}
							
						}else if(url.indexOf(",") != -1){
							String urlStrs[] = url.split(",");
							if (urlStrs != null && urlStrs.length > 0) {
								for (String string : urlStrs) {
									// 执行请求
									exeStep(paramMap,method, httpClient,
											element2, string, afterHandler);
								
								}
							}
						}
						else {
							// 执行请求使用用户默认的 URL
							 exeStep(paramMap,method, httpClient, element2,
									url, afterHandler);
						}

					}
				}
			} else {
				// 引发一个新的运行时异常,这样的话就会跳转到相应的500 提示界面
				throw new RuntimeException("error:没有与该爬取动作对应的配置文件");
			}
			// 保存本次请求结束的所有cookies 方便第二次用同一身份 再次请求 主要用于解决验证码问题
			String tmpcookies = "";
			// 获得登陆后的 Cookie
			Cookie[] cookies = httpClient.getState().getCookies();
			for (Cookie c : cookies) {
				tmpcookies += c.toString() + ";";

			}
			Object befCookies = paramMap.get("cookies");
			if (befCookies != null) {
				tmpcookies += befCookies.toString();
			}
			paramMap.put("cookies", tmpcookies);
		} catch (Exception e) {
			e.printStackTrace();
			// 引发一个新的运行时异常,这样的话就会跳转到相应的500 提示界面
			throw new RuntimeException("error:" + describe + " 出现异常");
		} finally {
			paramMap = null;
			httpClient = null;
		}

		return returnMap;
	}


	
	/**
	 * 执行配置文件定义的步骤
	 * 
	 * @param request
	 * @param response
	 * @param beforReturnParam
	 * @param stpeAfterReturnParam
	 * @param httpClient
	 * @param element2
	 * @param url
	 * @param afterHandler
	 * @return
	 * @throws UnsupportedEncodingException
	 * @throws IOException
	 * @throws HttpException
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws ClassNotFoundException
	 */
	private Map exeStep(Map paramMap,String methodtype, HttpClient httpClient, Element element2,
			String url, String afterHandler) throws Exception {
		// 获取当前步骤 执行前需要执行的 目标类
		String beforHandler = element2.attribute("BeforHandler") != null ? element2
				.attribute("BeforHandler").getValue()
				: null;
		// 判断是否执行步骤之前需要执行某函数
		if (StringUtils.isNotBlank(beforHandler)) {
			IBeforOrAfterHandler handlerFace=handlerMap.get(beforHandler);
			if(handlerFace==null){
				handlerFace=(IBeforOrAfterHandler) Class.forName(
						beforHandler).newInstance();
				handlerMap.put(beforHandler,handlerFace);
				
			}
			//可能会有参数注入，比如spring 后面实现吧
			//---
			paramMap = handlerFace.invoke(null, paramMap);
		}
		List<Element> parmElement = element2.element("params") != null ? element2
				.element("params").elements()
				: null;
		List<Element> cookiesElement = element2.element("headers") != null ? element2
				.element("headers").elements()
				: null;
		String HTML = "";
		if (StringUtils.isNotBlank(url)) {
			// 处理参数
			NameValuePair[] nvps = processParams(paramMap, parmElement);
			
			HttpMethodBase method=getMethodObj(paramMap,methodtype, httpClient, url, nvps);
			// 处理自定义header
			processHeader(cookiesElement, method, paramMap);
			// 执行请求
			httpClient.executeMethod(method);
			// 获取返回的参数
			
			HTML = Utils.inputStream2String(method.getResponseBodyAsStream());
		}
		// System.out.println(HTML);
		// 判断是否需要执行之后的handler
		if (StringUtils.isNotBlank(afterHandler)) {
			IBeforOrAfterHandler handlerFace=handlerMap.get(afterHandler);
			if(handlerFace==null){
				handlerFace=(IBeforOrAfterHandler) Class.forName(
						afterHandler).newInstance();
				handlerMap.put(afterHandler,handlerFace);
				
			}
			//可能会有参数注入，比如spring 后面实现吧
			//---
			paramMap = handlerFace.invoke(HTML, paramMap);
		}
		return paramMap;
	}

	

	/**
	 * 处理httpClient 请求中需要的cookie
	 * 
	 * @param cookiesElement
	 * @param postMethod
	 * @param httpClient
	 */
	private void processHeader(List<Element> cookiesElement,
			HttpMethodBase method, Map paramMap) {
		if (null != cookiesElement) {
			for (Element element3 : cookiesElement) {
				String headerkey = element3.element("header-key").getData() != null ? element3
						.element("header-key").getData().toString()
						: null;
				String headervalue = element3.element("header-value").getData() != null ? element3
						.element("header-value").getData().toString()
						: null;
				if (StringUtils.isNotBlank(headerkey)
						&& StringUtils.isNotBlank(headervalue)) {
					// 判断是否从步骤执行前的函数返回结果中取得结果
					if (headervalue.indexOf("#map") != -1) {
						headervalue = headervalue.substring(5, headervalue
								.length());
						headervalue = paramMap.get(headervalue) != null ? paramMap
								.get(headervalue).toString()
								: "";
					}
					// 判断是否从请求但中取值 为了兼容非web 暂时不支持request取值
					/*
					 * if(headervalue.indexOf("#request")!=-1){
					 * headervalue=headervalue.substring(9,headervalue.length());
					 * headervalue=request.getParameter(headervalue); }
					 */
					// 设置自定义header
					method.setRequestHeader(headerkey, headervalue);
				}
			}
		}
	}

	/**
	 * 处理每一个步骤请求时候需要的参数
	 * 
	 * @param request
	 * @param beforReturnParam
	 * @param stpeAfterReturnParam
	 * @param parmElement
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	private NameValuePair[] processParams(Map paramMap,
			List<Element> parmElement) throws UnsupportedEncodingException {
		if (null == parmElement) {
			return null;
		}
		NameValuePair[] params = new NameValuePair[parmElement.size()];
		int paranIndex = 0;
		for (Element element3 : parmElement) {
			// 获取编码格式
			String encoding = element3.attribute("encoding") != null ? element3
					.attribute("encoding").getValue() : null;
			String paramKey = element3.element("param-key").getData() != null ? element3
					.element("param-key").getData().toString()
					: null;
			String paramValue = element3.element("param-value") != null ? element3
					.element("param-value").getData().toString()
					: null;

			// 判断是否从请求但中取值
			/*
			 * if(paramValue.indexOf("#request")!=-1){
			 * paramValue=paramValue.substring(9,paramValue.length());
			 * paramValue=request.getParameter(paramValue); }
			 */
			// 判断是否从请求但中取值
			/*
			 * if(paramKey.indexOf("#request")!=-1){
			 * paramKey=paramKey.substring(9,paramKey.length());
			 * paramKey=request.getParameter(paramKey); }
			 */
			// 判断是否从步骤执行前的函数返回结果中取得结果
			if (paramValue != null && paramValue.indexOf("#map") != -1) {
				paramValue = paramValue.substring(5, paramValue.length());
				paramValue = paramMap.get(paramValue) != null ? paramMap.get(
						paramValue).toString() : "";
			}
			// 判断是否从步骤执行前的函数返回结果中取得结果
			if (paramKey != null && paramKey.indexOf("#map") != -1) {
				paramKey = paramKey.substring(5, paramKey.length());
				paramKey = paramMap.get(paramKey) != null ? paramMap.get(
						paramKey).toString() : "";
			}
			// 判断是否需要对参数进行 编码
			if (StringUtils.isNotBlank(encoding)) {
				paramValue = URLEncoder.encode(paramValue, encoding);
			}
			// 创建请求的需要参数对象
			NameValuePair nvp = new NameValuePair(paramKey, paramValue);
			params[paranIndex] = nvp;
			paranIndex++;
		}
		return params;
	}

	/**
	 * 
	 * 根据httpclient 对象获取 Post Method 的请求对象
	 */
	private HttpMethodBase getMethodObj(Map<String, Object> paramMap,String methodtype,
			HttpClient httpClient, String url, NameValuePair[] nvps) {
		HttpMethodBase method=null;
		if("post".equals(methodtype)){
			PostMethod post=new PostMethod(url);
			if (nvps != null && nvps.length > 0) {
				post.setRequestBody(nvps);	
			}
			method = post;	
		}else{
			method = new GetMethod(url);
		}
		
		// 设置 HttpClient 接收 Cookie,用与浏览器一样的策略
		httpClient.getParams().setCookiePolicy(
				CookiePolicy.BROWSER_COMPATIBILITY);
		
		method
				.setRequestHeader(
						"Accept",
						"image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, */*");
		method.setRequestHeader("Accept-Language", "zh-cn");
		method
				.setRequestHeader(
						"User-Agent",
						"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)");
		method.setRequestHeader("Content-Type",
				"application/x-www-form-urlencoded");
		// post.setRequestHeader("Accept-Encoding", "gzip, deflate");
		method.setRequestHeader("Connection", "Keep-Alive");
		method.setRequestHeader("Cache-Control", "no-cache");
		String tmpcookies = "";
		// 获得登陆后的 Cookie
		Cookie[] cookies = httpClient.getState().getCookies();

		for (Cookie c : cookies) {
			tmpcookies += c.toString() + ";";

		}

		// 判断请求中是否带有cookies
		String cookieBySession = (paramMap.get(RequestConfig.COOKIES) == null ? ""
				: (String) paramMap.get(RequestConfig.COOKIES));
		String isAddCookieBySession = (paramMap
				.get(RequestConfig.ISADDCOOKIEBYSESSION) == null ? ""
				: (String) paramMap.get(RequestConfig.ISADDCOOKIEBYSESSION));
		// 判断是否需要加上 session中的cookie
		if (StringUtils.isNotBlank(cookieBySession)
				&& StringUtils.isNotBlank(isAddCookieBySession)) {
			tmpcookies = tmpcookies + cookieBySession;
		}
		// 加上上一次请求返回的cookie
		if (StringUtils.isNotBlank(tmpcookies)) {
			method.setRequestHeader("Cookie", tmpcookies);
		}
		return method;
	}

	/**
	 * 根据属性明获取对应的值
	 * 
	 * @param rp
	 */
	private Object getParamByName(Serializable rp, String name) {

		if (null != name) {
			name = "get"
					+ name.replaceFirst(name.substring(0, 1), name.substring(0,
							1).toUpperCase());
		}
		try {
			if (rp != null) {
				if (rp instanceof String) {
					return (String) rp;
				}
				return rp.getClass().getMethod(name, null).invoke(rp, null);
			}
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 处理逻辑判断
	 * 
	 * @param args1
	 *            表达式参数 例如 1==1||2==1||#map.key==
	 * @param testMap
	 * @param flag
	 * @return
	 */
	private static boolean processLogicalJudgement(String expression,
			Map testMap, boolean flag) {
		String[] args1 = expression.split("\\|\\|");
		// 循环有多少或者的条件
		for (int i = 0; i < args1.length; i++) {
			if (args1[i].indexOf("&&") != -1) {
				String[] andS = args1[i].split("&&");
				// 循环所有的并且条件
				for (int j = 0; j < andS.length; j++) {
					String isEq = "";
					if (andS[j].indexOf("!=") != -1) {
						isEq = "!=";
					}
					if (andS[j].indexOf("==") != -1) {
						isEq = "==";
					}
					String[] arg = andS[j].split(isEq);
					// 判断表达式必须有两个值用于比较
					if (arg.length == 2) {
						String leftArg = arg[0];
						String rightArg = arg[1];

						Object leftArgOBj = leftArg;
						Object rightArgObj = rightArg;

						if (leftArg.indexOf("'") != -1
								|| rightArg.indexOf("'") != -1) {

							if (leftArg.indexOf("#map") != -1) {
								leftArgOBj = testMap.get(leftArg.substring(5,
										leftArg.length()));
							}
							if (rightArg.indexOf("#map") != -1) {
								rightArgObj = testMap.get(rightArg.substring(5,
										rightArg.length()));
							}
							if (leftArg.indexOf("'") != -1) {
								leftArgOBj = leftArg.substring(leftArg.indexOf(
										"'", 0), leftArg.lastIndexOf(","));
								System.out.println(leftArgOBj);
							}
							if (rightArg.indexOf("'") != -1) {
								rightArgObj = rightArg.substring(rightArg
										.indexOf("'", 0) + 1, rightArg
										.lastIndexOf("'"));
							}

							if (isEq.equals("==")) {
								if (leftArgOBj != null && rightArgObj != null
										&& rightArgObj.equals(leftArgOBj)) {
									System.out.println("成立了" + leftArgOBj);
									flag = true;
								} else {
									flag = false;
									// 当并且的条件中有一个不成立那么就直接跳出不比较下面的了
									break;
								}
							}
							if (isEq.equals("!=")) {
								if (leftArgOBj != null && rightArgObj != null
										&& !rightArgObj.equals(leftArgOBj)) {
									System.out.println("成立了" + leftArgOBj);
									flag = true;
								} else {
									flag = false;
									// 当并且的条件中有一个不成立那么就直接跳出不比较下面的了
									break;
								}
							}
						} else {
							if (leftArg.indexOf("#map") != -1) {
								leftArgOBj = testMap.get(leftArg.substring(5,
										leftArg.length()));
							} else {
								if (leftArgOBj != null) {
									if (leftArgOBj.toString().equals("true")
											|| leftArgOBj.toString().equals(
													"false")) {
										leftArgOBj = Boolean
												.parseBoolean(leftArgOBj
														.toString());
									}
									if (leftArgOBj.toString().equals("null")) {
										leftArgOBj = null;
									}
								} else {
									leftArgOBj = Integer.parseInt(leftArgOBj
											.toString());
								}
							}
							if (rightArg.indexOf("#map") != -1) {
								rightArgObj = testMap.get(rightArg.substring(5,
										rightArg.length()));
							} else {
								if (rightArgObj != null) {
									if (rightArgObj.toString().equals("true")
											|| rightArgObj.toString().equals(
													"false")) {
										rightArgObj = Boolean
												.parseBoolean(rightArgObj
														.toString());
									}
									if (rightArgObj.toString().equals("null")) {
										rightArgObj = null;
									}
								} else {
									rightArgObj = Integer.parseInt(rightArgObj
											.toString());
								}
							}
							if (isEq.equals("==")) {
								if (leftArgOBj == rightArgObj) {
									flag = true;
									System.out.println("成立了" + rightArgObj
											+ ":" + leftArgOBj);
								} else {
									flag = false;
									break;
								}
							} else {
								if (leftArgOBj != rightArgObj) {
									flag = true;
									System.out.println("成立了" + rightArgObj
											+ ":" + leftArgOBj);
								} else {
									flag = false;
									break;
								}
							}
						}
					} else {
						flag = false;
						break;
					}
				}
			} else {

				// 如果没有and 的情况下那么就比较其中的一个表达式成立的就 返回true
				String isEq = "";
				if (args1[i].indexOf("!=") != -1) {
					isEq = "!=";
				}
				if (args1[i].indexOf("==") != -1) {
					isEq = "==";
				}
				String[] arg = args1[i].split(isEq);
				// 判断表达式必须有两个值用于比较
				if (arg.length == 2) {
					String leftArg = arg[0];
					String rightArg = arg[1];

					Object leftArgOBj = leftArg;
					Object rightArgObj = rightArg;

					if (leftArg.indexOf("'") != -1
							|| rightArg.indexOf("'") != -1) {

						if (leftArg.indexOf("#map") != -1) {
							leftArgOBj = testMap.get(leftArg.substring(5,
									leftArg.length()));
						}
						if (rightArg.indexOf("#map") != -1) {
							rightArgObj = testMap.get(rightArg.substring(5,
									rightArg.length()));
						}
						if (leftArg.indexOf("'") != -1) {
							leftArgOBj = leftArg.substring(leftArg.indexOf("'",
									0), leftArg.lastIndexOf(","));
							System.out.println(leftArgOBj);
						}
						if (rightArg.indexOf("'") != -1) {
							rightArgObj = rightArg.substring(rightArg.indexOf(
									"'", 0) + 1, rightArg.lastIndexOf("'"));
						}
						if (isEq.equals("==")) {
							if (leftArgOBj != null && rightArgObj != null
									&& rightArgObj.equals(leftArgOBj)) {
								System.out.println("成立了" + leftArgOBj);
								flag = true;
							} else {
								flag = false;
								// 当并且的条件中有一个不成立那么就直接跳出不比较下面的了
								break;
							}
						}
						if (isEq.equals("!=")) {
							if (leftArgOBj != null && rightArgObj != null
									&& !rightArgObj.equals(leftArgOBj)) {
								System.out.println("成立了" + leftArgOBj);
								flag = true;
							} else {
								flag = false;
								// 当并且的条件中有一个不成立那么就直接跳出不比较下面的了
								break;
							}
						}
					} else {
						if (leftArg.indexOf("#map") != -1) {
							leftArgOBj = testMap.get(leftArg.substring(5,
									leftArg.length()));
						} else {
							if (leftArgOBj != null) {
								if (leftArgOBj.toString().equals("true")
										|| leftArgOBj.toString()
												.equals("false")) {
									leftArgOBj = Boolean
											.parseBoolean(leftArgOBj.toString());
								}
								if (leftArgOBj.toString().equals("null")) {
									leftArgOBj = null;
								}
							} else {
								leftArgOBj = Integer.parseInt(leftArgOBj
										.toString());
							}
						}
						if (rightArg.indexOf("#map") != -1) {
							rightArgObj = testMap.get(rightArg.substring(5,
									rightArg.length()));
						} else {
							if (rightArgObj != null) {
								if (rightArgObj.toString().equals("true")
										|| rightArgObj.toString().equals(
												"false")) {
									rightArgObj = Boolean
											.parseBoolean(rightArgObj
													.toString());
								}
								if (rightArgObj.toString().equals("null")) {
									rightArgObj = null;
								}
							} else {
								rightArgObj = Integer.parseInt(rightArgObj
										.toString());
							}
						}

						if (isEq.equals("==")) {
							if (leftArgOBj == rightArgObj) {
								flag = true;
								System.out.println("成立了" + rightArgObj + ":"
										+ leftArgOBj);
							} else {
								flag = false;
								break;
							}
						} else {
							if (leftArgOBj != rightArgObj) {
								flag = true;
								System.out.println("成立了" + rightArgObj + ":"
										+ leftArgOBj);
							} else {
								flag = false;
								break;
							}
						}
					}
				} else {
					flag = false;
				}
			}
		}
		return flag;
	}

	public HttpClient getHttpClient() {
		return httpClient;
	}

	public void setHttpClient(HttpClient httpClient) {
		this.httpClient = httpClient;
	}
}
